<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterBatchNumber.query"
        :filter-list="filterBatchNumber.searchList"
        :filter-btn="filterBatchNumber.searchBtn"
      />
      <page-table
        :refresh="tableBatchNumber.refresh"
        :init-curpage="tableBatchNumber.initCurpage"
        v-model:data="tableBatchNumber.data"
        :api="tableBatchNumber.api"
        :listenHeight="false"
        :reserveSelection="true"
        :query="filterBatchNumber.query"
        :field-list="tableBatchNumber.fieldList"
        :handle="tableBatchNumber.handle"
        :highlight-row="highlightRow"
        :check-box="checkBox"
        @handleEvent="handleEventBatchNumber"
      >
        <template v-slot:col-stockQty="scope">
          <span>{{
            scope.row.frozenQty
              ? $fn.qtyFormatter(scope.row.stockQty - scope.row.frozenQty)
              : $fn.qtyFormatter(scope.row.stockQty)
          }}</span>
        </template>
      </page-table>
    </page-dialog>
  </div>
</template>

<script setup name="selectBatchNumber">
import PageDialog from '../PageDialog/index'
import PageTable from '../PageTable/index'
import PageFilter from '../NewPageFilter/index'
import { getDepotStoragePage } from '@/api/stock.js'
import { getCustomizeSetting } from '@/api/customize'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getDepotStoragePage,
  },
  tableKey: {
    type: String,
    default:
      'productPageWithBatchNumber-com.slip.spd.entity.vo.stock.DepotStockVO',
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})
const filterBatchNumber = ref({
  query: {
    stockQtyGreater: 0,
    queryMap: {
      commonName: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      brandCode: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      brandName: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      calcUnitName: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      codeType: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      licenseNumber: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      mnemonic: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      productCode: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      standard: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      unitName: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      batchNumber: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      supplierName: {
        queryType: 'RIGHT_LIKE',
        queryCategory: 'AND',
        value: '',
      },
      productionDate: {
        queryType: 'IN',
        queryCategory: 'AND',
        value: '',
      },
      validityDate: {
        queryType: 'IN',
        queryCategory: 'AND',
        value: '',
      },
    },
    customizeUserVO: {},
  },
  searchList: [
    {
      type: 'input',
      label: '名称',
      value: 'commonName',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '批号',
      value: 'batchNumber',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '厂家编码',
      value: 'brandCode',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '厂家名称',
      value: 'brandName',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '计算单位',
      value: 'calcUnitName',
      event: search,
      show: false,
    },
    {
      value: 'codeType',
      type: 'select-model',
      label: '条码类型',
      checkDefault: false,
      event: search,
      clearable: true,
      multiple: true,
      code: 'other_store_order_status',
      show: false,
    },
    {
      type: 'input',
      label: '证号',
      value: 'licenseNumber',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '助记符',
      value: 'mnemonic',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '产品编码',
      value: 'productCode',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '规格',
      value: 'standard',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '计量单位',
      value: 'unitName',
      event: search,
      show: false,
    },
    {
      type: 'input',
      label: '供应商',
      value: 'supplierName',
      event: search,
      show: false,
    },
    {
      type: 'date',
      label: '生产日期',
      value: 'productionDate',
      event: search,
      show: false,
    },
    {
      type: 'date',
      label: '生产日期',
      value: 'validityDate',
      event: search,
      show: false,
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
const tableBatchNumber = ref({
  refresh: 1,
  initCurpage: 1,
  height: '70%',
  api: undefined,
  data: [],
  fieldList: [
    { label: '别名', value: 'alias', hidden: true },
    { label: '批号', value: 'batchNumber', hidden: true },
    { label: '厂家编码', value: 'brandCode', hidden: true },
    { label: '厂家名称', value: 'brandName', hidden: true },
    { label: '计算单位', value: 'calcUnitName', hidden: true },
    { label: '条码类型', value: 'codeTypeValue', hidden: true },
    { label: '通用名称', value: 'commonName', hidden: true },
    { label: '仓库', value: 'depotName', hidden: true },
    { label: '冻结数量', value: 'frozenQty', hidden: true },
    { label: '单价', value: 'price', hidden: true },
    { label: '生产日期', value: 'productionDate', hidden: true },
    { label: '展示价格', value: 'showPrice', hidden: true },
    { label: '库存数量', value: 'stockQty', hidden: true },
    { label: '供应商', value: 'supplierName', hidden: true },
    { label: '失效日期', value: 'validityDate', hidden: true },
    { label: '证号', value: 'licenseNumber', hidden: true },
    { label: '助记符', value: 'mnemonic', hidden: true },
    { label: '产品编码', value: 'productCode', hidden: true },
    { label: '产品状态', value: 'productStatus', hidden: true },
    { label: '产品类型', value: 'productType', hidden: true },
    { label: '规格', value: 'standard', hidden: true },
    { label: '单位换算系数', value: 'unitConvertCoefficient', hidden: true },
    { label: '计量单位', value: 'unitName', hidden: true },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择产品批号',
  btLoading: false,
  width: '75%',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
  get() {
    return showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})
// 获取批号列表
function getListBatchNumber() {
  tableBatchNumber.value.refresh = Math.random()
}
function search() {
  tableBatchNumber.value.refresh = Math.random()
  tableBatchNumber.value.initCurpage = Math.random()
}
function save() {
  // emits('input', data)
  if (selectRow.value) {
    handleClose()
    let selectRow = selectRow.value.map((row) => {
      return {
        ...row,
        stockQty: row.stockQty - (row.frozenQty ? row.frozenQty : 0),
      }
    })
    emits('callback', selectRow)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择批号',
    })
  }
}
function handleEventBatchNumber(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.value.btList[1].disabled = false
      } else {
        dialogInfo.value.btList[1].disabled = true
      }
      break
    case 'list':
      data = data.map((row) => {
        return {
          ...row,
          stockQty: row.stockQty - (row.frozenQty ? row.frozenQty : 0),
        }
      })
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let tableInfo = tableBatchNumber.value
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      )
      filterBatchNumber.value.query.queryMap = {
        ...filterBatchNumber.value.query.queryMap,
        ...props.query,
      }
      tableInfo.api = props.api
      //   getListBatchNumber();
    }
  })
}
onMounted(() => {
  getCustomize()
})
</script>

<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
